//------------------------------------------------------------------------------------
#include <QtDebug>
#include <QFile>
#include "CSoundPlayer.h"
//------------------------------------------------------------------------------------
CSoundPlayer::CSoundPlayer(QString source, QObject *parent) : QObject(parent) {
	QFile file(source);

	mediaObject = 0;

	if(file.open(QIODevice::ReadOnly)) {
		byteArray=new QByteArray(file.readAll());
		file.close();

		mediaObject = new Phonon::MediaObject();
		audioOutput = new Phonon::AudioOutput(Phonon::GameCategory);
		Phonon::createPath(mediaObject, audioOutput);

		buffer = new QBuffer(byteArray);
		buffer->open(QIODevice::ReadOnly);

		mediaSource = new Phonon::MediaSource(buffer);

		mediaObject->setCurrentSource(*mediaSource);

		connect(mediaObject, SIGNAL(finished()), this, SLOT(onFinished()));
	}
}
//------------------------------------------------------------------------------------
CSoundPlayer::~CSoundPlayer(void) {
	if(mediaObject != 0) {
		mediaObject->stop();

		delete mediaSource;
		delete byteArray;
		delete buffer;
		delete audioOutput;
		delete mediaObject;
	}
}
//------------------------------------------------------------------------------------
void CSoundPlayer::play(bool boucle) {
	this->boucle = boucle;
	if(mediaObject != 0) {
		mediaObject->play();
	}
}
//------------------------------------------------------------------------------------
void CSoundPlayer::stop(void) {
	if(mediaObject != 0) {
		mediaObject->stop();
	}
}
//------------------------------------------------------------------------------------
void CSoundPlayer::onFinished(void) {
	if(boucle && mediaObject != 0) {
		mediaObject->play();
	}
}
//------------------------------------------------------------------------------------
